package com.atkxp.sys.controller;

import com.atkxp.sys.annotation.Log;
import com.atkxp.sys.entity.Menu;
import com.atkxp.sys.entity.Result;
import com.atkxp.sys.entity.User;
import com.atkxp.sys.enums.BusinessType;
import com.atkxp.sys.service.IMenuService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kxp
 * @since 2023-08-26
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @GetMapping("/list")
    public Result<List<Menu>> getAllMenu() {
        return Result.success(menuService.getAllMenu(), "查询成功");
    }

    @GetMapping("/menuList")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<List<Menu>> getMenuList(@RequestParam(value = "title", required = false) String name) {
//        System.out.println(name);
        return Result.success(menuService.getMenuList(name), "查询成功");
    }

    @GetMapping("/getMenuByMenuId/{menuId}")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    public Result<Menu> getMenuByMenuId(@PathVariable("menuId") Integer menuId) {
//        System.out.println(menuService.getById(menuId));
        return Result.success(menuService.getById(menuId), "查询成功");
    }

    @PostMapping("/updateMenu")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    public Result<?> updateMenu(@RequestBody  Menu menu) {
        if(menu.getMenuType().equals("M")) {
            menu.setIsLeaf("N");
        } else {
            menu.setIsLeaf("Y");
        }
        menuService.updateById(menu);
        return Result.success("更新成功！");
    }

    @PostMapping("/addMenu")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @Transactional
    public Result<?> addMenu(@RequestBody Menu menu) {
        if(menu.getParentId() != 0) {
            if(menu.getMenuType().equals("M")) {
                menu.setName(menu.getTitle());
                menu.setComponent(null);
                menu.setIsLeaf("N");
            } else {
                menu.setName(menu.getTitle());
                menu.setIsLeaf("Y");
            }
        } else {
            if(menu.getMenuType().equals("M")) {
                menu.setName(menu.getTitle());
                menu.setComponent("Layout");
                menu.setIsLeaf("N");
            } else {
                menu.setName(menu.getTitle());
                menu.setIsLeaf("Y");
            }
        }
//        System.out.println(menu);
        menuService.save(menu);

        return Result.success("增加成功！");
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:menu:list')")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    public Result<?> deleteMenuById(@PathVariable("id") Integer id) {
        menuService.deleteMenuById(id);
        return Result.success("删除成功！");
    }
}
